package com.aqiang.day0528_shop.main.view.activity

import android.content.ContentValues
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import com.aqiang.day0528_shop.main.contract.GoodsTypeContract
import com.aqiang.day0528_shop.sql.SearchSql
import com.aqiang.lib_core.mvp.BaseActivity
import kotlinx.android.synthetic.main.activity_search.*
import android.view.Gravity
import android.widget.TextView
import android.graphics.Color
import android.support.v4.content.ContextCompat
import com.aqiang.day0528_shop.R
import org.jetbrains.anko.startActivity


class SearchActivity : BaseActivity<GoodsTypeContract.GoodsTypePresenter>() {
    private lateinit var searchSql:SearchSql
    override fun bindLatout(): Int {
        return com.aqiang.day0528_shop.R.layout.activity_search
    }

    override fun createPresenter() {

    }

    override fun initView(savedInstanceState: Bundle?) {

    }

    override fun initData() {
        searchSql = SearchSql(this)
        searchSql.use {
            val cursor = query("search", null, null, null, null, null, null)
            if(cursor != null){
                while (cursor.moveToNext()){
                    val name = cursor.getString(cursor.getColumnIndex("name"))
                    val textView = TextView(this@SearchActivity)
                    textView.setText(name)
                    textView.setTextColor(Color.WHITE)
                    textView.setBackgroundDrawable(ContextCompat.getDrawable(this@SearchActivity,
                        com.aqiang.day0528_shop.R.drawable.tv_lable))
                    textView.gravity = Gravity.CENTER
                    textView.setPadding(20, 0, 10, 0)
                    textView.textSize = 16f
                    label_act_search_host.addView(textView)
                }
            }
        }
    }

    override fun initEvent() {
        iv_act_srarch_del.setOnClickListener {
            searchSql.use {
                delete("search",null,null)
            }
            label_act_search_host.removeAllViews()
        }
        et_act_search.addTextChangedListener(object : TextWatcher{
            override fun afterTextChanged(s: Editable?) {

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

        })

        tv_act_seatch.setOnClickListener {
            searchSql.use {
                val contentValues = ContentValues()
                contentValues.put("name",et_act_search.text.toString())
                insert("search",null,contentValues)
//                val textView = TextView(this@SearchActivity)
//                textView.setText(s.toString())
//                textView.setTextColor(Color.WHITE)
//                textView.setBackgroundDrawable(ContextCompat.getDrawable(this@SearchActivity,
//                    com.aqiang.day0528_shop.R.drawable.tv_lable))
//                textView.gravity = Gravity.CENTER
//                textView.setPadding(20, 0, 10, 0)
//                textView.textSize = 16f
//                label_act_search_host.addView(textView)
            }
            startActivity<GoodsActivity>(
                "name" to et_act_search.text.toString()
            )
            overridePendingTransition(R.anim.anim_in,R.anim.anim_out)
        }
    }

}
